<template>
  <!-- ref/child -->
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
      <div class="pullup-tips" v-show="pullUpLoad">
        <div v-if="!isPullUpLoad" class="before-trigger">
          <!-- <span class="pullup-txt">Pull up and load more</span> -->
        </div>
        <div v-else class="after-trigger">
          <span class="pullup-txt">Loading...</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";
export default {
  props: {
    probeType: {
      type: Number,
      default: 0,
    },
    pullUpLoad: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      scroll: null,
      isPullUpLoad: false,
    };
  },
  methods: {
    getScrollY() {
      return this.scroll ? this.scroll.y : 0;
    },
    scrollTo(x, y, time) {
      //逻辑&& 先判断前面的值有效 在执行后面的
      this.scroll && this.scroll.scrollTo(x, y, time);
    },
    finishPullUp() {
      this.scroll.finishPullUp();
      // this.scroll.refresh();
      this.isPullUpLoad = false;

      // this.scroll.refresh();
    },
    refresh() {
      this.scroll && this.scroll.refresh();
    },
  },

  mounted() {
    // let wrapper = document.querySelector(".wrapper");
    //1、创建BSCROLL对象
    this.scroll = new BScroll(this.$refs.wrapper, {
      observeDOM: true,
      click: true,
      taps: true,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad,
    });
    //2、监听滚动的位置
    if (this.probeType == 2 || this.probeType == 3) {
      this.scroll.on("scroll", (position) => {
        // console.log(position);
        this.$emit("scroll", position);
      });
    }
    //3、监听scroll滚动到底部
    if (this.pullUpLoad) {
      this.scroll.on("pullingUp", () => {
        this.$emit("pullingUp");
        this.isPullUpLoad = true;
      });
    }
  },
};
</script>

<style>
.pullup-tips {
  padding: 20px;
  text-align: center;
  color: #999;
}
</style>